package com.longshihan.hostapp.home;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.longshihan.hostapp.Constants;
import com.longshihan.hostapp.R;
import com.longshihan.hostapp.SharePreferencesHelper;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LONGHE001.
 *
 * @time 2019/9/3 0003
 * @des
 * @function
 */

public class PluginListAdapter extends RecyclerView.Adapter {
    public List<PluginModel> pluginModels;
    public OnPluginLoadListener loadListener;
    private Context context;
    Gson gson2=new Gson();
    public PluginListAdapter(Context context,OnPluginLoadListener loadListener) {
        pluginModels = new ArrayList<>();
        this.loadListener = loadListener;
        this.context=context;
    }

    public void setPluginModels(List<PluginModel> pluginModels) {
        this.pluginModels = pluginModels;
        String str=gson2.toJson(pluginModels);
        SharePreferencesHelper.put(context, Constants.pluginData,str);
        notifyDataSetChanged();
    }

    public void addPluginModels(PluginModel pluginModel) {
        this.pluginModels.add(pluginModel);
        String str=gson2.toJson(pluginModels);
        SharePreferencesHelper.put(context, Constants.pluginData,str);
        notifyDataSetChanged();
    }

    public void removePluginModels(PluginModel pluginModel) {
        this.pluginModels.remove(pluginModel);
        String str=gson2.toJson(pluginModels);
        SharePreferencesHelper.put(context, Constants.pluginData,str);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_homeplugin, parent, false);
        PluginViewHolder mViewHolder = new PluginViewHolder(view);
        return mViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final PluginViewHolder pluginViewHolder = (PluginViewHolder) holder;
        final PluginModel pluginModel = pluginModels.get(position);
        pluginViewHolder.addr.setText(pluginModel.addr);
        pluginViewHolder.name.setText(pluginModel.name);
        if (pluginModel.isLoad) {
            if (pluginModel.isUpgrade){
                pluginViewHolder.load.setText("升级插件");
            }else {
                pluginViewHolder.load.setText("卸载插件");
            }
        } else {
            pluginViewHolder.load.setText("安装插件");
        }
        pluginViewHolder.load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loadListener != null) {
                    if (pluginModel.isLoad) {
                        loadListener.onunInstallPluginInfo(pluginModel);
                    } else {
                        loadListener.onInstallPluginInfo(pluginModel);
                    }
                }
            }
        });
        pluginViewHolder.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loadListener != null) {
                    if (pluginModel.isLoad) {
                        loadListener.onStartActivity(pluginModel, "");
                    } else {
                        Toast.makeText(pluginViewHolder.context, "未安装插件，请先安装", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return pluginModels.size();
    }

    public interface OnPluginLoadListener {
        void onInstallPluginInfo(PluginModel model);

        void onunInstallPluginInfo(PluginModel model);

        void onStartActivity(PluginModel pluginModel, String activityName);
    }
}
